package com.xjl.meituwidget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import java.io.File;

public class MeituWidgetProvider extends AppWidgetProvider {
    private static final String TAG = "MeituWidgetProvider";
    public static final String CLICK_ACTION = "com.xjl.meituwidget.action.CLICK"; // 点击事件的广播ACTION
    RemoteViews remoteViews;

    /**
     * 每次窗口小部件被更新都调用一次该方法
     */
    @Override
    public void onUpdate (Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.i(TAG, "onUpdate");
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        Intent intent = new Intent(CLICK_ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.iv, pendingIntent);

        final String path = SpHelper.getInstance().getString("path");
        Log.i(TAG, "path:" + path);
        final File filePath = new File(path);
        final File[] files = filePath.listFiles();
        if (files != null) {
            Log.i(TAG, "img_path:" + files[0].getPath());
            final Bitmap bitmap = BitmapFactory.decodeFile(files[0].getPath());
            remoteViews.setImageViewBitmap(R.id.iv, bitmap);
        }
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }

    /**
     * 接收窗口小部件点击时发送的广播
     */
    @Override
    public void onReceive (Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.i(TAG, "onReceive");
        if (CLICK_ACTION.equals(intent.getAction())) {
            Toast.makeText(context, "hello dog!", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 每删除一次窗口小部件就调用一次
     */
    @Override
    public void onDeleted (Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        Log.i(TAG, "onDeleted");
    }

    /**
     * 当最后一个该窗口小部件删除时调用该方法
     */
    @Override
    public void onDisabled (Context context) {
        super.onDisabled(context);
        Log.i(TAG, "onDisabled");
    }

    /**
     * 当该窗口小部件第一次添加到桌面时调用该方法
     */
    @Override
    public void onEnabled (Context context) {
        super.onEnabled(context);
        Log.i(TAG, "onEnabled");
    }

    /**
     * 当小部件大小改变时
     */
    @Override
    public void onAppWidgetOptionsChanged (Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
        Log.i(TAG, "onAppWidgetOptionsChanged");
        final String path = SpHelper.getInstance().getString("path");
        Log.i(TAG, "path:" + path);
        final File filePath = new File(path);
        Log.i(TAG, "filePath exists:" + filePath.exists());
        final File[] files = filePath.listFiles();
        if (files != null) {
            Log.i(TAG, "img_path:" + files[0].getPath());
            final Bitmap bitmap = BitmapFactory.decodeFile(files[0].getPath());
            remoteViews.setImageViewBitmap(R.id.iv, bitmap);
        }
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }

    /**
     * 当小部件从备份恢复时调用该方法
     */
    @Override
    public void onRestored (Context context, int[] oldWidgetIds, int[] newWidgetIds) {
        super.onRestored(context, oldWidgetIds, newWidgetIds);
        Log.i(TAG, "onRestored");
    }
}
